package com.tz.liveplayermodule;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.PowerManager;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

import com.tz.liveplayermodule.util.CommonUtils;

public class FullScreenLiveVideoActivity extends Activity {

    private VideoView videoView;
    private LinearLayout loadingView;
    private MediaController mController;
    private MediaPlayer mediaPlayer;
    String videoPath;
    ImageButton fullScreenImage;
    private PowerManager.WakeLock wakeLock;
    private int playCurrent;

    @SuppressLint("WrongConstant")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        CommonUtils.keepScreenOn(wakeLock, this, true);

        setContentView(R.layout.activity_full_screen_live_video);

        findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishWithData();
            }
        });

        videoView = findViewById(R.id.video_view);
        loadingView = findViewById(R.id.loading_view);
        mController = new MediaController(this);
        videoView.setMediaController(mController);
        mController.setMediaPlayer(videoView);
        loadingView.setVisibility(View.VISIBLE);

        fullScreenImage = findViewById(R.id.full_screen_image);
        fullScreenImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finishWithData();
            }
        });


        videoPath = getIntent().getStringExtra("filePath");
        playCurrent = getIntent().getIntExtra("Current", 0);

        if (getIntent().getFlags() == 10001) {
            setupVideo();
        } else {
            videoView.setVideoPath(videoPath);
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mMediaPlayer) {
                    loadingView.setVisibility(View.GONE);
                    mediaPlayer = mMediaPlayer;
                    mediaPlayer.start();
                    videoView.seekTo(playCurrent);
                }
            });
        }

        ImageView change_size_img = (ImageView) findViewById(R.id.change_size_img);
        final LinearLayout change_size_layout = (LinearLayout) findViewById(R.id.change_size_layout);
        ImageView change_size_img1 = (ImageView) findViewById(R.id.change_size_img1);
        ImageView change_size_img2 = (ImageView) findViewById(R.id.change_size_img2);
        ImageView change_size_img3 = (ImageView) findViewById(R.id.change_size_img3);
        ImageView change_size_img4 = (ImageView) findViewById(R.id.change_size_img4);
        change_size_img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.VISIBLE);
            }
        });
        change_size_img1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                setPlaySpeed(1.0f);
            }
        });
        change_size_img2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                setPlaySpeed(1.25f);
            }
        });
        change_size_img3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                setPlaySpeed(1.5f);
            }
        });
        change_size_img4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                change_size_layout.setVisibility(View.GONE);
                setPlaySpeed(2.0f);
            }
        });

    }

    private void setPlaySpeed(float speed) {
        if (mediaPlayer != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                if (mediaPlayer.isPlaying()) {
                    mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed));
                } else {
                    mediaPlayer.setPlaybackParams(mediaPlayer.getPlaybackParams().setSpeed(speed));
                    mediaPlayer.pause();
                }
            } else {
                Toast.makeText(FullScreenLiveVideoActivity.this, "您的手机版本过低，无法倍数播放", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(FullScreenLiveVideoActivity.this, "视频加载中，请稍后再试", Toast.LENGTH_SHORT).show();
        }
    }

    private void finishWithData() {
        Bundle bundle = new Bundle();
        bundle.putInt("Current", videoView.getCurrentPosition());
        Intent intent = new Intent();
        intent.putExtras(bundle);
        setResult(RESULT_OK, intent);
        finish();
    }

    private void setupVideo() {
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mp) {
                loadingView.setVisibility(View.GONE);
                mediaPlayer = mp;
                mediaPlayer.start();

                videoView.start();
            }
        });
        videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                stopPlaybackVideo();
            }
        });
        videoView.setOnErrorListener(new MediaPlayer.OnErrorListener() {
            @Override
            public boolean onError(MediaPlayer mp, int what, int extra) {
                stopPlaybackVideo();
                return true;
            }
        });

        try {
            Uri uri = Uri.parse(videoPath);
            videoView.setVideoURI(uri);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void stopPlaybackVideo() {
        try {
            videoView.stopPlayback();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequiresApi(api = Build.VERSION_CODES.HONEYCOMB)
    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopPlaybackVideo();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        System.out.println("onRestart");
        if (!CommonUtils.isEmpty(videoPath)) {
            loadingView.setVisibility(View.VISIBLE);
            videoView.setVideoPath(videoPath);
            videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
                @Override
                public void onPrepared(MediaPlayer mMediaPlayer) {
                    loadingView.setVisibility(View.GONE);
                    mediaPlayer = mMediaPlayer;
                    mediaPlayer.start();
                    videoView.seekTo(playCurrent);
                }
            });
        }
        CommonUtils.keepScreenOn(wakeLock, this, true);
        CommonUtils.keepScreenLongLight(this, true);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (videoView.canPause()) {
            videoView.pause();
        }
        playCurrent = videoView.getCurrentPosition();
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (!videoView.isPlaying()) {
            videoView.resume();
        }
    }

}
